﻿Imports DAL

Public Class buContacts


    ''' <summary>
    ''' Truy vấn thông tin liên hệ dựa vào ID khách hàng
    ''' </summary>
    ''' <param name="customerId">ID khách hàng</param>
    Public Function RetrieveContactsByCustomerId(ByVal customerId As Integer) As List(Of Contact)

        'FUNC: Truy vấn DANH SÁCH Contact dựa vào Customer Id - List(Of T) kết hợp LINQ (WHERE)
        Using dbEntities As New PrestozEntities

            Return dbEntities.Contacts.Where(Function(c) c.CustomerId = customerId).ToList()

        End Using

    End Function


    ''' <summary>
    ''' Truy vấn thông tin liên hệ dựa vào ID
    ''' </summary>
    ''' <param name="contactId">ID liên hệ</param>
    Public Function RetrieveContactById(ByVal contactId As Integer) As Contact

        Using dbEntities As New PrestozEntities

            Return (From c In dbEntities.Contacts
                    Where c.ContactId = contactId
                    Select c).FirstOrDefault()
        End Using
    End Function


    ''' <summary>
    ''' Cập nhật record Contact
    ''' </summary>
    ''' <param name="contact">Dữ liệu để cập nhật</param>
    Public Function UpdateContactById(ByVal contact As Contact) As Boolean

        Using dbEntities As New PrestozEntities
            Dim contactId As Integer = contact.ContactId
            'Lấy dữ liệu contact tương ứng với ID chỉ định
            Dim retrieveContact As New Contact

            retrieveContact = dbEntities.Contacts.FirstOrDefault(Function(c) c.ContactId = contactId)

            Try
                'Cập nhật các thông tin
                retrieveContact.Title = contact.Title
                retrieveContact.Name = contact.Name
                retrieveContact.JobTitle = contact.JobTitle
                retrieveContact.Department = contact.Department
                retrieveContact.CustomerId = contact.CustomerId
                retrieveContact.Mobile = contact.Mobile
                retrieveContact.Email = contact.Email
                retrieveContact.Telephone = contact.Telephone
                retrieveContact.Note = contact.Note

                retrieveContact.LastUpdate = DateTime.Now

                dbEntities.SaveChanges()

                Return True

            Catch ex As Exception
                MsgBox(ex.Message)
                dbEntities.ClearChanges()

                Return False
            End Try
        End Using

    End Function


    ''' <summary>
    ''' Truy vấn danh sách ID liên hệ
    ''' </summary>
    Public Function RetrieveListOfContactIds() As List(Of Contact)

        Using dbEntities As New PrestozEntities
            Dim contactIds As List(Of Contact) = (From c In dbEntities.Contacts
                                                Select New Contact With {
                                                    .ContactId = c.ContactId}).ToList()

            Return contactIds
        End Using

    End Function


    ''' <summary>
    ''' Thêm mới một record Contact
    ''' </summary>
    ''' <param name="contact">Dữ liệu Contact mới</param>
    Public Function InsertNewContact(ByVal contact As Contact) As Boolean

        Using dbEntities As New PrestozEntities

            Dim retrieveContact As New Contact

            'Ghi nhận các thông tin
            retrieveContact.Title = contact.Title
            retrieveContact.Name = contact.Name
            retrieveContact.JobTitle = contact.JobTitle
            retrieveContact.Department = contact.Department
            retrieveContact.CustomerId = contact.CustomerId
            retrieveContact.Mobile = contact.Mobile
            retrieveContact.Email = contact.Email
            retrieveContact.Telephone = contact.Telephone
            retrieveContact.Note = contact.Note

            retrieveContact.CreatedDate = DateTime.Now
            retrieveContact.LastUpdate = DateTime.Now

            Try

                dbEntities.Add(retrieveContact)
                dbEntities.SaveChanges()

                Return True

            Catch ex As Exception
                MsgBox(ex.Message)
                dbEntities.ClearChanges()

                Return False
            End Try
        End Using

    End Function


    ''' <summary>
    ''' Xóa record contact dựa vào ID
    ''' </summary>
    ''' <param name="contactId">ID liên hệ</param>
    Public Function DeleteContactById(ByVal contactId As Integer) As Boolean

        Using dbEntities As New PrestozEntities

            Try
                dbEntities.Delete(From c In dbEntities.Contacts
                              Where c.ContactId = contactId
                              Select c)
                dbEntities.SaveChanges()

                Return True

            Catch ex As Exception

                MsgBox(ex.Message)
                dbEntities.ClearChanges()

                Return False

            End Try
        End Using

    End Function


End Class